package com.jiangjiang.eshop.commodity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jiangjiang.eshop.commodity.dto.PropertyDTO;
import com.jiangjiang.eshop.commodity.dto.PropertyPageQueryDTO;
import com.jiangjiang.eshop.commodity.dto.PropertySaveDTO;
import com.jiangjiang.eshop.commodity.dto.PropertyUpdateDTO;
import com.jiangjiang.eshop.commodity.domain.PropertyEntity;
import com.jiangjiang.eshop.commodity.operation.Properties;
import com.jiangjiang.eshop.commodity.service.PropertyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Jianghaowen
 * @Date 2021/6/1
 * @Description: TODO
 * @Version V1.0
 **/
@Slf4j
@RestController
@RequestMapping("/commodity/property")
public class PropertyController {

    @Autowired
    private PropertyService propertyService;

    /**
     * 属性列表分页查询
     *
     * @param dto 分页查询dto
     * @return 查询结果
     */
    @PostMapping("/queryForPage")
    public IPage<PropertyEntity> queryForPage(@RequestBody PropertyPageQueryDTO dto) {
        try {
            return propertyService.queryForPage(dto);
        } catch (Exception e) {
            log.warn("PropertyController queryForPage error",e);
            return null;
        }
    }
    /**
     * 新增属性
     *
     * @param dto 新增属性DTO
     * @return 查询结果
     */
    @PostMapping("/save")
    public Boolean saveProperty(@RequestBody PropertySaveDTO dto) {
        try {
            return propertyService.saveProperty(dto);
        }catch (Exception e){
            log.warn("PropertyController saveProperty error", e);
            return false;
        }
    }

    /**
     * 更新属性
     * @param dto 属性更新DTO
     * @return 处理结果
     */
    @PostMapping("/update")
    public Boolean updateProperty(@RequestBody PropertyUpdateDTO dto) {
        try {
            return propertyService.updateProperty(dto);
        }catch (Exception e){
            log.warn("PropertyController updateProperty error", e);
            return false;
        }
    }

    /**
     * 根据id查询接口
     * @param id 属性id
     * @return 查询结果
     */
    @GetMapping("/getById")
    public PropertyDTO queryById(Long id){
        try {
            return propertyService.queryById(id);
        } catch (Exception e){
            log.warn("PropertyController queryById error",e);
            return null;
        }
    }

    /**
     * 根据类目id获取类目关联的所有属性
     * @param categoryId 类目id
     * @return 查询结果
     */
    @GetMapping("/getPropertiesByCategoryId")
    public Properties getPropertiesByCategoryId(@RequestParam("categoryId")Long categoryId){
        try {
            return propertyService.getPropertiesByCategoryId(categoryId);
        }catch (Exception e){
            log.warn("PropertyController getPropertiesByCategory error",e);
        }
        return new Properties();
    }
}
